package com.fxsh168.server.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.fxsh168.server.R;
import com.fxsh168.server.entitys.UserTranslatinRecord;

import java.util.List;

/**
 * 自定义消息adapter类
 */
public class UserRecordAdapter extends BaseAdapter {
    private List<UserTranslatinRecord.DataBean> listData;//数据源
    private Context context;//上下文
    public UserRecordAdapter(List<UserTranslatinRecord.DataBean> listData, Context context) {
        this.listData = listData;
        this.context = context;
    }

    public UserRecordAdapter() {
    }

    @Override
    public int getCount() {
        return listData == null ? 0 : listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_record, null);
            viewHolder = new ViewHolder();
            viewHolder.contentMain = (TextView) convertView.findViewById(R.id.contentMain);
            viewHolder.blanceMain = (TextView) convertView.findViewById(R.id.blanceMain);
            viewHolder.timeMain = (TextView) convertView.findViewById(R.id.timeMain);
            convertView.setTag(viewHolder);//给convertView设置标签
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过标签获得viewHolder
        }

        //获得数据
        UserTranslatinRecord.DataBean dataBean = listData.get(position);
        viewHolder.contentMain.setText(dataBean.getContent());
        String ad = listData.get(position).getAd();
        if (ad.equals("1")) {
            viewHolder.blanceMain.setText("+" + dataBean.getBalance());
        } else {
            viewHolder.blanceMain.setText("-" + dataBean.getBalance());
        }
        viewHolder.timeMain.setText(dataBean.getTime());

        return convertView;
    }


    //创建ViewHolder内部类
    class ViewHolder {
        TextView contentMain, blanceMain, timeMain;
    }
}
